package com.twu.biblioteca;

import com.twu.biblioteca.Goods.Book;
import com.twu.biblioteca.Goods.Movie;
import com.twu.biblioteca.User.User;
import com.twu.biblioteca.exception.InvalidOptionException;
import com.twu.biblioteca.factory.ActionFactory;
import com.twu.biblioteca.factory.BasicAction;

import java.util.InputMismatchException;

public class BibliotecaApp {
    private Shelf bookShelf = new Shelf();
    private Shelf movieShelf = new Shelf();
    private User currentUser;
    private Receiver receiver = new Receiver();

    public BibliotecaApp() {
        initBooks();
        initMovies();
    }

    private void initBooks() {
        bookShelf.add(new Book("C++ Primer"));
        bookShelf.add(new Book("A Ghost Story"));
        bookShelf.add(new Book("Oliver Twist"));
    }

    private void initMovies() {
        movieShelf.add(new Movie("Broken City", "2013", "Allen Hughes", "10"));
        movieShelf.add(new Movie("The Wolf of Wall Street", "2013", "Martin Scorsese", "12"));
        movieShelf.add(new Movie("12 Years a Slave", "2013", "Steve McQueen", "10"));
    }

    public Shelf getBookShelf() {
        return bookShelf;
    }

    public Shelf getMovieShelf() {
        return movieShelf;
    }

    public Action chooseFunction() {
        Println.chooseFunctionPrintln(getCurrentUser().isLibriran());
        int chosenFunction = 0;
        while (true) {
            try {
                chosenFunction =receiver.getDataInt();
                Action action = Action.from(chosenFunction, getCurrentUser().isLibriran());
                if (action == null) {
                    throw new InvalidOptionException();
                } else {
                    return action;
                }
            } catch (InputMismatchException exception) {
                receiver.getData();
                Println.println(Println.INVALID_OPTION);
                continue;
            } catch (InvalidOptionException e) {
                Println.println(Println.INVALID_OPTION);
                continue;
            }

        }
    }

    public void run() {
        while (true) {
            Println.println(Println.WELCOME);
            Println.println("(Now Only Jane is libririan)");
            Println.println(Println.INPUT_ACCOUNT);
            String userName = receiver.getData();
            Println.println(Println.INPUT_PASSWORD);
            String password = receiver.getData();
            logIn(userName,password);
            ActionFactory actionFactory = new ActionFactory();
            BasicAction basicAction = actionFactory.createAction(chooseFunction());
            basicAction.run(this, receiver);
        }
    }

    public static void main(String[] args) {
        BibliotecaApp bibliotecaApp = new BibliotecaApp();
        bibliotecaApp.run();
    }

    public void logIn(String account, String password) {
        //Todo add new user as Librriaian and will be deleted in the future.
        //Todo not check the password valid
        if ("Jane".equals(account)) {
            this.currentUser = new User(account, true, password);
        } else {
            this.currentUser = new User(account, false, password);
        }
    }

    public User getCurrentUser() {
        return currentUser;
    }
}
